#include <iostream>
#include <stdio.h>
#include <assert.h>
#include <cstdlib>

typedef int SlistDataType;

typedef struct SListNode
{
    SlistDataType data;
    struct SListNode* next;
}SListNode;


SListNode* CreateList()
{
    SListNode* phead, *ptail;
    phead = (SListNode*)malloc(sizeof(SListNode));
    if(phead == nullptr)
    {
        printf("malloc fail!");
        return nullptr;
    }
    ptail = phead;
    ptail->next = nullptr;
    int len = 0;
    std::cout << "请输入插入结点的个数" << std::endl;
    for(int i = 0; i < len; i++)
    {
        SListNode* pnew = (SListNode*)malloc(sizeof(SListNode));
        if(pnew == nullptr)
        {
            printf("新结点创建失败!");
            exit(-1);
        }

        std::cout << "请输入结点数据-> " << std::endl;
        scanf("%d", &pnew->data);
        pnew->next = nullptr;
        ptail->next = pnew;
        ptail = pnew;
    }
    std::cout << "创建成功!" << std::endl;
    return phead->next;
}

void TraverseList(SListNode* phead)
{
    SListNode* p = phead;
    std::cout << "遍历链表的值为->" << std::endl;
    if(p == nullptr)
    {
        std::cout << "链表为空" << std::endl;
    }

    while(p != nullptr)
    {
        std::cout << p->data << " ";
        p = p->next;
    }
    std::cout << std::endl;
}

int main()
{
	SListNode* List = CreateList();//链表创建好后，用List接收头结点位置
	TraverseList(List);//将头结点传入，打印该链表
	return 0;
}
